[% setvar title Angle brackets should not be used for file globbing %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Angle brackets should not be used for file globbing</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Jon Ericson &lt;<a href='mailto:Jonathan.L.Ericson@jpl.nasa.gov'>Jonathan.L.Ericson@jpl.nasa.gov</a>&gt;
  Date: 4 Aug 2000
  Last Modified: 30 Aug 2000
  Mailing List: <a href='mailto:perl6-language-io@perl.org'>perl6-language-io@perl.org</a>
  Number: 34
  Version: 3
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Angle brackets have two separate meanings in Perl - file globbing and
line input from a filehandle.  This means that perl attempts to Do What
I Mean and often misinterprets me.  Since file globbing can be
accomplished with the glob function and since file input is the more
common use of angle brackets, they should not be used for file globbing.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='Background'></a><h2>Background</h2>
<p>If angle brackets contain is a simple scalar variable (e.g., &lt;$foo&gt;),
then that variable contains the name of the filehandle to input from, or
its typeglob, or a reference to the same.  For example:</p>
<pre>           $fh = \*STDIN;
           $line = &lt;$fh&gt;;</pre>
<p>If what's within the angle brackets is neither a filehandle nor a simple
scalar variable containing a filehandle name, typeglob, or typeglob
reference, it is interpreted as a filename pattern to be globbed, and
either a list of filenames or the next filename in the list is returned,
depending on context.  This distinction is determined on syntactic
grounds alone.  That means `&lt;$x&gt;' is always a readline() from an
indirect handle, but `&lt;$hash{key}&gt;' is always a glob().  That's because
$x is a simple scalar variable, but `$hash{key}' is not--it's a hash
element.</p>
<p>One level of double-quote interpretation is done first, but you can't
say `&lt;$foo&gt;' because that's an indirect filehandle as explained in the
previous paragraph.  (In older versions of Perl, programmers would
insert curly brackets to force interpretation as a filename glob:
`&lt;${foo}&gt;'.  These days, it's considered cleaner to call the internal
function directly as `glob($foo)', which is probably the right way to
have done it in the first place.)  For example:</p>
<pre>           while (&lt;*.c&gt;) {
               chmod 0644, $_;
           }</pre>
<p>is roughly equivalent to:</p>
<pre>           open(FOO, &quot;echo *.c | tr -s ' \t\r\f'
'\\012\\012\\012\\012'|&quot;);
           while (&lt;FOO&gt;) {
               chop;
               chmod 0644, $_;
           }</pre>
<p>except that the globbing is actually done internally using the standard
`File::Glob' extension.  Of course, the shortest way to do the above is:</p>
<pre>           chmod 0644, &lt;*.c&gt;;</pre>
<p>A (file)glob evaluates its (embedded) argument only when it is starting
a new list.  All values must be read before it will start over.  In list
context, this isn't important because you automatically get them all
anyway.  However, in scalar context the operator returns the next value
each time it's called, or C run out.  As with filehandle reads, an
automatic `defined' is generated when the glob occurs in the test part
of a `while', because legal glob returns (e.g. a file called 0) would
otherwise terminate the loop.  Again, `undef' is returned only once.  So
if you're expecting a single value from a glob, it is much better to say</p>
<pre>           ($file) = &lt;blurch*&gt;;</pre>
<p>than</p>
<pre>           $file = &lt;blurch*&gt;;</pre>
<p>because the latter will alternate between returning a filename and
returning false.</p>
<p>It you're trying to do variable interpolation, it's definitely better
to use the glob() function, because the older notation can cause people
to become confused with the indirect filehandle notation.</p>
<pre>           @files = glob(&quot;$dir/*.[ch]&quot;);
           @files = glob($files[$i]);</pre>
<p>-- perlop/&quot;I/O Operators&quot;</p>
<a name='Areas of confusion'></a><h2>Areas of confusion</h2>
<ul>
<li><a name='Complex filehandle references'></a>Complex filehandle references</li>
<pre>    my %filesystem;
    my $filename = '/etc/shells';
    open $filesystem{$filename}, $filename 
        or die &quot;can't open $filename: $!&quot;;
    print &lt;$filesystem{$filename}&gt;;
    __END__

    GLOB{0xa042284}</pre>
<li><a name='Simple file glob variables'></a>Simple file glob variables</li>
<pre>    my $fileglob = '/etc/sh*';
    print &lt;$fileglob&gt;;
    __END__

    [nothing printed]</pre>
<li><a name='Why don't these have the same output?'></a>Why don't these have the same output?</li>
<pre>    $ perl -e 'print &lt;/etc/sh*&gt;'
    /etc/shells

    $ perl -e 'print &lt;&gt;' /etc/sh*
    /bin/sh
    /bin/bash</pre>
</ul>
<a name='The glob function exists'></a><h2>The glob function exists</h2>
<p>Now that we have a function interface to file globbing, why do we need
the obscure operator interface?</p>
<a name='The readline meaning is more common'></a><h2>The readline meaning is more common</h2>
<p>Angle brackets are a shortcut for either glob() or readline().  If we
are going to eliminate one meaning, it should be the less common
meaning.</p>
<a name='Syntax niche could be filled with a simple, flexible input operator'></a><h2>Syntax niche could be filled with a simple, flexible input
operator</h2>
<p>RFC 51 suggests that:</p>
<pre>  print &lt;'/etc/shells'&gt;;</pre>
<p>should print the content of the /etc/shells file, not:</p>
<pre>  '/etc/shells'</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Remove the file-globbing behavior of angle brackets.</p>
<a name='Migration from Perl 5'></a><h2>Migration from Perl 5</h2>
<p>Scripts which use &lt;&gt; for globbing will need to be modified to use the
glob function instead.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<ul>
<li><a name='v3'></a>v3</li>
<pre>  Changed status to 'Frozen' because there was no discussion 
  on the previous version.  I assume that this is an 
  uncontroversial RFC.</pre>
<li><a name='v2'></a>v2</li>
<pre>  Changed mailing list to language-io
  Modified the format of the perlop quote
  Added the Migration implementation section
  Added RFC 51 references</pre>
</ul>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>perlop/&quot;I/O Operators&quot;</p>
<p>perlfunc/glob</p>
<p>perlfunc/readline</p>
<p>RFC 51: Angle brackets should accept filenames and lists</p>
</div>
